﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;

namespace SafeLoad.NControl
{
	/// <summary>
	/// NTankMin.xaml 的交互逻辑
	/// </summary>
	public partial class NTankMin : UserControl
	{
		public NTankMin()
		{
			InitializeComponent();
		}

		/// <summary>
		/// 舱室的标题
		/// </summary>
		public static readonly DependencyProperty TankTitleProperty = DependencyProperty.Register("TankTitle",
		  typeof(string), typeof(NTankMin), new PropertyMetadata(""));

		public string TankTitle
		{
			get { return (string)GetValue(TankTitleProperty); }
			set { SetValue(TankTitleProperty, value); }
		}

		/// <summary>
		/// 舱室的背景颜色
		/// </summary>
		/// 
		public static readonly DependencyProperty FrameBackgroundColorProperty = DependencyProperty.Register("FrameBackgroundColor",
		  typeof(Color), typeof(NTankMin), new PropertyMetadata(/*Colors.LightGray, */new PropertyChangedCallback(RerenderBinding)));
		public Color FrameBackgroundColor
		{
			get
			{
				return (Color)GetValue(FrameBackgroundColorProperty);
			}
			set
			{
				SetValue(FrameBackgroundColorProperty, value);
			}
		}

		/// <summary>
		/// 当前容量，单位立方米
		/// </summary>
		public static readonly DependencyProperty VolumeValueProperty = DependencyProperty.Register("VolumeValue",
		  typeof(double), typeof(NTankMin), new PropertyMetadata(0.0d, new PropertyChangedCallback(RerenderBinding)));
		public double VolumeValue
		{
			get { return (double)GetValue(VolumeValueProperty); }
			set { SetValue(VolumeValueProperty, value); }
		}

		public static readonly DependencyProperty TotalVolumeProperty = DependencyProperty.Register("TotalVolume",
		  typeof(double), typeof(NTankMin), new PropertyMetadata(0.0d, new PropertyChangedCallback(RerenderBinding)));
		/// <summary>
		/// 总容量，单位立方米
		/// </summary>
		public double TotalVolume
		{
			get { return (double)GetValue(TotalVolumeProperty); }
			set { SetValue(TotalVolumeProperty, value); }
		}

		/// <summary>
		/// 液位高度 单位 米
		/// </summary>
		public static readonly DependencyProperty LevelValueProperty = DependencyProperty.Register("LevelValue",
		  typeof(double), typeof(NTankMin), new PropertyMetadata(0.0d));
		public double LevelValue
		{
			get { return (double)GetValue(LevelValueProperty); }
			set 
			{
				if (value > 0)
				{
					double a = 0;
				}
				SetValue(LevelValueProperty, value);
			}
		}

		/// <summary>
		/// 装卸速率 单位 立方米每小时
		/// </summary>
		public static readonly DependencyProperty RateValueProperty = DependencyProperty.Register("RateValue",
		  typeof(double), typeof(NTankMin), new PropertyMetadata(0.0d));
		public double RateValue
		{
			get { return (double)GetValue(RateValueProperty); }
			set { SetValue(RateValueProperty, value); }
		}

		/// <summary>
		/// 容量百分比高度
		/// </summary>
		public static readonly DependencyProperty VolumePercentHeightProperty = DependencyProperty.Register("VolumePercentHeight",
		  typeof(int), typeof(NTankMin), new PropertyMetadata(0));
		public int VolumePercentHeight
		{
			get { return (int)GetValue(VolumePercentHeightProperty); }
			set { SetValue(VolumePercentHeightProperty, value); }
		}

		/// <summary>
		/// 容量百分比
		/// </summary>
		public static readonly DependencyProperty VolumePercentValueProperty = DependencyProperty.Register("VolumePercentValue",
		  typeof(double), typeof(NTankMin), new PropertyMetadata(0.0d));
		public double VolumePercentValue
		{
			get { return (double)GetValue(VolumePercentValueProperty); }
			set { SetValue(VolumePercentValueProperty, value); }
		}

		/// <summary>
		/// 报警状态，0 正常(绿色), 1 黄色， 2 橙色， 3 红色 
		/// </summary>
		public static readonly DependencyProperty AlarmStateProperty = DependencyProperty.Register("AlarmState",
			typeof(int), typeof(NTankMin), new PropertyMetadata(0));
		public int AlarmState
		{
			get { return (int)GetValue(AlarmStateProperty); }
			set { SetValue(AlarmStateProperty, value); }
		}

		/// <summary>
		/// 状态显示的颜色
		/// </summary>
		public static readonly DependencyProperty AlarmStateColorProperty = DependencyProperty.Register("AlarmStateColor",
			typeof(Color), typeof(NTankMin), new PropertyMetadata(Colors.Green));
		public Color AlarmStateColor
		{
			get { return (Color)GetValue(AlarmStateColorProperty); }
			set { SetValue(AlarmStateColorProperty, value); }
		}

		///// <summary>
		///// 设置是否显示状态指示
		///// </summary>
		public static readonly DependencyProperty ShowStateProperty =
			DependencyProperty.Register("ShowState", typeof(bool), typeof(NTankMin), new PropertyMetadata(true));
		public bool ShowState
        {
            get { return (bool)GetValue(ShowStateProperty); }
            set { SetValue(ShowStateProperty, value); }
        }

		/// <summary>
		/// 装载率报警灯的颜色
		/// </summary>
		public static readonly DependencyProperty VolumeAlarmColorProperty = DependencyProperty.Register("VolumeAlarmColor",
		  typeof(Color), typeof(NTankMin), new PropertyMetadata(Colors.GreenYellow));
		public Color VolumeAlarmColor
		{
			get { return (Color)GetValue(VolumeAlarmColorProperty); }
			set { SetValue(VolumeAlarmColorProperty, value); }
		}

		/// <summary>
		/// 装卸速率报警颜色
		/// </summary>
		public static readonly DependencyProperty RateAlarmColorProperty = DependencyProperty.Register("RateAlarmColor",
			typeof(Color), typeof(NTankMin), new PropertyMetadata(Colors.GreenYellow));
		public Color RateAlarmColor
		{
			get { return (Color)GetValue(RateAlarmColorProperty); }
			set { SetValue(RateAlarmColorProperty, value); }
		}       
		
		///// <summary>
				///// 设置是否显示选中
				///// </summary>
		public static readonly DependencyProperty IsSelectedProperty =
			DependencyProperty.Register("IsSelected", typeof(bool), typeof(NTankMin), new PropertyMetadata(false));
		public bool IsSelected
        {
            get { return (bool)GetValue(IsSelectedProperty); }
            set { SetValue(IsSelectedProperty, value); }
        }

        private static void RerenderBinding(DependencyObject d, DependencyPropertyChangedEventArgs e)
		{
			NTankMin gauge = d as NTankMin;
			gauge.TankUpdate();
		}

		private void TankUpdate()
		{

			double percent = 0;
			if (VolumeValue > 0.000001 && TotalVolume > 0.000001)
			{
				percent = VolumeValue / TotalVolume;
				percent = Math.Min(percent, 1);
				percent = Math.Max(percent, 0);
			}

			double oldVolumePercentValue = VolumePercentValue;
			VolumePercentValue = percent * 100;
			VolumePercentHeight = (int)(percent * (RecPercentContainer.Height - 2));

			FrameBackgroundColor = Colors.LightGreen;

			if (VolumePercentValue > 95)
				VolumeAlarmColor = Colors.Red;
			if (TotalVolume > 0 && RateValue >= TotalVolume)
				RateAlarmColor = Colors.Red;

			//if (RateValue > 10.0)
			//	FrameBackgroundColor = Colors.LightGreen;
			//else if (RateValue < -10.0)
			//	FrameBackgroundColor = Colors.Yellow;
			//else
			//	FrameBackgroundColor = Colors.LightGray;

			//if (Math.Abs(oldVolumePercentValue - VolumePercentValue) > 0)
			//	FrameBackgroundColor = Colors.LightGreen;
			//else
			//	FrameBackgroundColor = Colors.LightGray;
		}
	}
}
